package com.gxa.modules.pc.hospital.controller;

import com.gxa.common.exception.ResultException;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.app.hospital.health.service.HealthService01;
import com.gxa.modules.model.entity.hospital.Health;
import com.gxa.modules.oss.factory.OSSFactory;
import com.gxa.modules.pc.hospital.service.HealthService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 健康百科文章表 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:04:59
 */
@Api(tags = "健康百科")
@RestController
@RequestMapping("/pc/health")
@Slf4j
public class HealthController {

    //前端app
    @Autowired
    private HealthService01 healthService01;

    //后端
    @Autowired
    private HealthService healthService;

    //时间转换器
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation(value = "带条件的分页查询")
    //TODO 分页查询返回的数组不能注解中文
//    @ApiResponses({
//            @ApiResponse(response = )
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "totalCount",value ="总记录数",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "pageSize",value ="每页显示多少条",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "title",value ="查询条件（文章标题）",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "totalPage",value ="总页数",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "currPage",value ="当前是第几页",dataType ="int"),

    })
    @GetMapping
    public Result<PageUtils> queryByPage (@RequestParam @ApiIgnore Map<String,Object> params){
        PageUtils pageUtils = this.healthService01.queryByTitle(params);

        return new Result<PageUtils>().ok(pageUtils);
    }


//    @ApiOperation(value = "删除")
//    @ApiImplicitParam(paramType = "path",name = "ids",value ="要删除数据的id集合",dataType ="List")
//    @DeleteMapping
//    public Result delById (@RequestParam("ids") List<Long> ids){
//
//        String s = healthService.delByIds(ids);
//
//        return new Result().ok(s);
//    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(paramType = "body",name = "ids",value ="要删除数据的id集合",dataType ="List")
    @DeleteMapping
    public Result delById (@RequestBody List<Long> ids){

        String s = healthService.delByIds(ids);

        return new Result().ok(s);
    }


    @ApiOperation(value = "新增健康百科")
    @PostMapping
    public Result add(@RequestBody Health health){

        healthService.add(health);

        return new Result().ok();
    }

    @ApiOperation(value = "修改健康百科")
    @PutMapping
    public Result update(@RequestBody Health health){

        healthService.update(health);

        return new Result().ok();
    }

    @ApiOperation(value = "根据文章的id查询")
    @ApiImplicitParam(paramType = "query",name = "id",value ="文章的id",dataType ="Integer")
    @GetMapping("/query")
    public Result<Health> queryById(Integer id){
        Health health = healthService.queryById(id);
        return new Result<Health>().ok(health);
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("/up")
    public Result<String> upload(@RequestParam("file") MultipartFile file)throws Exception {
        if (file.isEmpty()) {
            throw new ResultException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return new Result<String>().ok(url);
    }






}
